#include <iostream>
#include <fstream>
#include "contacts.pb.h"

using namespace google::protobuf;

const std::string filename = "../contacts.bin";

void PrintContacts(contacts::Contacts& contacts)
{
    for (int i = 0; i < contacts.contacts_size(); i++)
    {
        std::cout << "--------------联系人" << i + 1 << "----------------" << std::endl;
        const contacts::PeopleInfo& people = contacts.contacts(i);
        std::cout << "联系人姓名:" << people.name() << std::endl;
        std::cout << "联系人年龄:" << people.age() << std::endl;
        for (int j = 0; j < people.phones_size(); j++)
        {
            const contacts::PeopleInfo_Phone& phone = people.phones(j);
            std::cout << "联系人电话" << j + 1 << ":" << phone.number() << std::endl;
        }

        const Reflection* reflection = contacts::PeopleInfo::GetReflection();\
        const UnknownFieldSet& set = reflection->GetUnknownFields(people);
        for (int j = 0; j < set.field_count(); j++)
        {
            const UnknownField& unknown_field = set.field(j);
            std::cout << "未知字段: ";
            std::cout << "编号: " << unknown_field.number();
            switch(unknown_field.type())
            {
                case UnknownField::Type::TYPE_VARINT:
                    std::cout << " 值: " << unknown_field.varint() << std::endl;
                    break;
                case UnknownField::Type::TYPE_LENGTH_DELIMITED:
                    std::cout << " 值: " << unknown_field.length_delimited() << std::endl;
                    break;
                // case ...
            }
        }
    } 
}

int main()
{
    contacts::Contacts contacts;
    std::fstream input(filename, std::ios::in | std::ios::binary);
    if (!contacts.ParseFromIstream(&input))
    {
        std::cout << "parse error..." << std::endl;
        return 1;
    }

    PrintContacts(contacts);
    return 0;
}